package com.hele.sts.dao;

import com.hele.sts.pub.ConnectDB;
import com.hele.sts.pub.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName BmDao
 * @Description TODO
 * @Author xuning
 * @Date 2019-03-28 22:44
 **/
@Repository
public class BmDao {

    private static final Logger logger = LoggerFactory.getLogger(BmDao.class);
    public static final String TABLE_GXDW = "bm_gxdw";
    public static final String TABLE_DJJG = "bm_djjg";
    public static final String TABLE_SPQX = "bm_spqx";
    public static final String TABLE_QYYY = "bm_qyyy";

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public  List getBmList(String bmlx)
    {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = null;
        ConnectDB connsyb = null;
        String content = null;
        List<Bm> list = new ArrayList<>();
        sql = "select code,content from "+bmlx +" order by code";

        list = this.jdbcTemplate.query(sql, new RowMapper<Bm>() {

            @Override
            public Bm mapRow(ResultSet rs, int rowNum) throws SQLException {
                Bm bm = new Bm();
                bm.setCode(rs.getString("code"));
                bm.setContent(rs.getString("content"));
                return bm;
            }
        });
//        try
////        {
////            connsyb = SpringContextHolder.getBean(ConnectDB.class);
////            conn = connsyb.getConnection();
////            pstmt = conn.prepareStatement(sql);
////            rs = pstmt.executeQuery();
////            while (rs.next()) {
////                Bm bm = new Bm();
////                bm.setCode(rs.getString("code").trim());
////                bm.setContent(rs.getString("content").trim());
////                list.add(bm);
////            }
////            return list;
////        }
////        catch (SQLException e)
////        {
////            logger.info("db error", e);
////        }
////        finally
////        {
////            DbClose.freePreparedStatement(pstmt);
////            DbClose.freeConnection(conn);
////        }
////
////        return null;
        return list;
    }
}
